//
//  YHStatusCell.swift
//  WeiBoYH
//
//  Created by yanhui on 2017/1/9.
//  Copyright © 2017年 yanhui. All rights reserved.
//

import UIKit

private let homeCellMargin: CGFloat = 8
//计算最大的显示宽度
private let maxWidth: CGFloat = ScreenWidth - 2 * homeCellMargin

//定义每张图片的间距
private let pictureCellMargin: CGFloat = 2
//计算每张图片显示的固定宽度
private let itemWidth: CGFloat = (maxWidth - 2 * pictureCellMargin) / 3

class YHStatusCell: UITableViewCell {
    
    //微博作者头像
    @IBOutlet weak var headIcon: UIImageView!
    //微博作者名称
    @IBOutlet weak var nameLabel: UILabel!
    //作者等级
    @IBOutlet weak var rankView: UIImageView!
    //微博时间
    @IBOutlet weak var timeLabel: UILabel!
    //微博来源
    @IBOutlet weak var sourceLabel: UILabel!
    //微博正文
    @IBOutlet weak var contentLabel: UILabel!
    //认证类型
    @IBOutlet weak var verifiedIcon: UIImageView!
    
    @IBOutlet weak var pictureView: YHPictureCollectionView!
    //高度的约束对象
    @IBOutlet weak var pictureHeightCons: NSLayoutConstraint!
    //宽度的约束对象
    @IBOutlet weak var pictureWidthCons: NSLayoutConstraint!
    //流水布局对象
    @IBOutlet weak var pictureFlowLayout: UICollectionViewFlowLayout!
    //视图模型
    var viewmodel: YHHomeCellModel? {
        didSet {
            //给控件绑定数据
            timeLabel.text = viewmodel?.status?.created_at
            sourceLabel.text = viewmodel?.status?.source
            contentLabel.text = viewmodel?.status?.text
            nameLabel.text = viewmodel?.status?.user?.screen_name
           
            headIcon.sd_setImage(with: viewmodel?.headURL)
            rankView.image = viewmodel?.mbrankImage
            verifiedIcon.image = viewmodel?.verifiedImage
            let count = viewmodel?.status?.pic_urls?.count ?? 0
            let pSize = calculatePictureViewSize(count: count)
            //更新约束值
            pictureHeightCons.constant = pSize.height
            pictureWidthCons.constant = pSize.width
            //设置数据源
            pictureView.pic_urls = viewmodel?.status?.pic_urls
            
        }
    }
    


    override func awakeFromNib() {
        super.awakeFromNib()
        //设置自动换行
        contentLabel.numberOfLines = 0
        contentLabel.preferredMaxLayoutWidth = ScreenWidth - 2 * homeCellMargin
        pictureView.backgroundColor = UIColor.randomColor
        pictureFlowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //设置间距
        pictureFlowLayout.minimumLineSpacing = pictureCellMargin
        pictureFlowLayout.minimumInteritemSpacing = pictureCellMargin
        
    }
    
    
    //根据配图视图的数量计算配图视图的大小
    private func calculatePictureViewSize(count: Int) -> CGSize {
        
        //1. 没有图片
        if count == 0 {
            return CGSize.zero
        }
        //2. 单张图片 
        if count == 1 {
            return CGSize(width: itemWidth, height: itemWidth)
        }
        //3. 四张图片
        if count == 4 {
            let width = itemWidth * 2.01 + pictureCellMargin
            return CGSize(width: width, height: width)
        }
                //根据图片的数量先确定有多少行
        let row = (count - 1) / 3 + 1
        //确定高度
        let h = itemWidth * CGFloat(row) + pictureCellMargin * (CGFloat(row) - 1)
        
        return CGSize(width: maxWidth, height: h)
    }
}
